package demo.compoment;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.BeanPostProcessor;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

//继承这个接口就是通知
public class BeanCompoment implements BeanNameAware,BeanPostProcessor {
    @Override
    public void setBeanName(String s) {//s为我们的Bean的名字
        System.out.println("执行了通知，BeanName为" + s);
    }
    @Override
    public Object postProcessBeforeInitialization(Object bean,String beanName) throws BeansException {
        System.out.println("初始化前置方法");
        return bean;
    }

    //接下来执行初始化方法，有两种，一种是在xml中用init-method方法，另外一种注解
    //在xml中定义的初始化方法名，要在相关类中实现
    public void myInit() {
        System.out.println("XML方法初始化");
    }

    @Override
    //初始化后置方法
    public Object postProcessAfterInitialization(Object bean,String beanName) throws BeansException {
        System.out.println("初始化方法后");
        return bean;
    }
    @PostConstruct
    public void doPostConstruct() {
        System.out.println("注解方式初始化");
    }

    public void sayHi() {
        System.out.println("执行sayHi()方法");
    }

    //销毁方式也有两种：一种是通过XML方式另外一个是通过注解
    @PreDestroy
    public void doPreDestroy() {
        System.out.println("doPreDestroy");
    }
}
